package com.crb.crm.manage.web.biz.shop;

import com.crb.crm.manage.entity.biz.ShopSearchDTO;
import com.crb.crm.manage.entity.biz.ShopVO;
import com.crb.crm.manage.pojo.User;
import com.crb.crm.manage.pojo.biz.Shop;
import com.crb.crm.manage.service.biz.ShopService;
import com.crb.crm.manage.utils.PageDataResult;
import com.idempotent.starter.annotations.FormPreIdempotent;
import com.idempotent.starter.annotations.Idempotent;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class ShopController {

    private static final Logger logger = LoggerFactory
            .getLogger(ShopController.class);
    @Autowired
    private ShopService shopService;

    @RequestMapping("/shopList")
    @FormPreIdempotent
    public String toShopList(HttpServletRequest request) {
       return "biz/shop/shopList";
    }

    /**
     * 分页查询商户列表
     *
     * @return ok/fail
     */
    @RequestMapping(value = "/getShops", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = "getShops")
    public PageDataResult getShops(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit, ShopSearchDTO shopSearch) {
        logger.debug("分页查询商户列表！搜索条件：shopSearch：" + shopSearch + ",page:" + page
                + ",每页记录数量limit:" + limit);
//		ErrorController
        PageDataResult pdr = new PageDataResult();
        try {
            if (null == page) {
                page = 1;
            }
            if (null == limit) {
                limit = 10;
            }
            Thread.sleep(2000);
            // 获取商户
            pdr = shopService.getShops(shopSearch, page, limit);
            logger.debug("商户列表查询=pdr:" + pdr);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("商户列表查询异常！", e);
        }
        return pdr;
    }


    /**
     * 设置商户[新增或更新]
     *
     * @return ok/fail
     */
    @RequestMapping(value = "/setShop", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions({"shopAdd", "shopEdit"})
    @Idempotent
    public String setShop(Shop shop) {
        logger.debug("设置商户[新增或更新]！shop:" + shop);
        try {
            if (null == shop) {
                logger.debug("设置商户[新增或更新]，结果=请您填写商户信息");
                return "请您填写商户信息";
            }
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            if (null == existUser) {
                logger.debug("设置商户[新增或更新]，结果=您未登录或登录超时，请您登录后再试");
                return "您未登录或登录超时，请您登录后再试";
            }
            //user.setInsertUid(existUser.getId());
            // 设置商户[新增或更新]
            Thread.sleep(2000);
            logger.info("设置商户[新增或更新]成功！shop=" + shop + "，操作的用户ID=" + existUser.getId());
            return shopService.setShop(shop);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("设置商户[新增或更新]异常！", e);
            return "操作异常，请您稍后再试";
        }
    }

    /**
     * 删除商户
     *
     * @return ok/fail
     */
    @RequestMapping(value = "/delShop", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("shopDelete")
    @Idempotent
    public String delShop(@RequestParam("id") Integer id) {
        logger.debug("删除商户！id:" + id);
        String msg = "";
        try {
            if (null == id) {
                logger.debug("删除商户，结果=请求参数有误，请您稍后再试");
                return "请求参数有误，请您稍后再试";
            }
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            if (null == existUser) {
                logger.debug("删除商户，结果=您未登录或登录超时，请您登录后再试");
                return "您未登录或登录超时，请您登录后再试";
            }
            Thread.sleep(2000);
            // 删除商户
            msg = shopService.setDelShop(id, 1);
            logger.info("删除商户:" + msg + "。userId=" + id + "，操作用户id:" + existUser.getId());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除商户异常！", e);
            msg = "操作异常，请您稍后再试";
        }
        return msg;
    }

    /**
     * @param id
     * @return
     * @描述：恢复商户
     * @创建人：wyait
     * @创建时间：2018年4月27日 上午9:49:14
     */
    @RequestMapping(value = "/recoverShop", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("shopDelete")
    @Idempotent
    public String recoverShop(@RequestParam("id") Integer id) {
        logger.debug("恢复商户！id:" + id);
        String msg = "";
        try {
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            if (null == existUser) {
                return "您未登录或登录超时，请您登录后再试";
            }
            if (null == id) {
                return "请求参数有误，请您稍后再试";
            }
            // 删除商户
            msg = shopService.setDelShop(id, 0);
            logger.info("恢复商户【" + this.getClass().getName() + ".recoverShop】"
                    + msg + "。商户Id=" + id + "，操作的用户ID=" + existUser.getId());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("恢复商户【" + this.getClass().getName()
                    + ".recoverUser】商户异常！", e);
            msg = "操作异常，请您稍后再试";
        }
        return msg;
    }

    /**
     * 查询商户数据
     *
     * @return map
     */
    @RequestMapping(value = "/getShop", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getShop(@RequestParam("id") Integer id) {
        logger.debug("查询商户数据！id:" + id);
        Map<String, Object> map = new HashMap<>();
        try {
            if (null == id) {
                logger.debug("查询商户数据==请求参数有误，请您稍后再试");
                map.put("msg", "请求参数有误，请您稍后再试");
                return map;
            }
            // 查询商户
            ShopVO shopvo = shopService.getShop(id);
            logger.debug("查询商户数据！shopvo=" + shopvo);
            if (null != shopvo) {
                map.put("shop", shopvo);
                map.put("msg", "ok");
            } else {
                map.put("msg", "查询商户信息有误，请您稍后再试");
            }
            logger.debug("查询商户数据成功！map=" + map);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "查询商户错误，请您稍后再试");
            logger.error("查询商户数据异常！", e);
        }
        return map;
    }


}
